package com.mrlee.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by lizhongren1.
 */
public class StrUtils {

    public static boolean isNull(String str) {

        if (str == null || str.length() == 0) {
            return true;
        } else {
            return false;
        }
    }

    public static boolean isIp(String str){
        String regEx = "^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\.(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\.(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\.(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)$";
        Pattern pattern = Pattern.compile(regEx);
        Matcher matcher = pattern.matcher(str);
        return matcher.matches();
    }

    public static boolean isLetterNum_Right(String str){
        String regEx = "^[0-9a-zA-Z_]{1,}$";
        Pattern pattern = Pattern.compile(regEx);
        Matcher matcher = pattern.matcher(str);
        return matcher.matches();
    }

    public static boolean isNumber(String str){
        String regEx = "^[0-9]*$";
        Pattern pattern = Pattern.compile(regEx);
        Matcher matcher = pattern.matcher(str);
        return matcher.matches();
    }

    public static boolean isChineseLetterNumRight(String str){
        String regEx = "^[A-Za-z0-9\\u4e00-\\u9fa5]+$";
        Pattern pattern = Pattern.compile(regEx);
        Matcher matcher = pattern.matcher(str);
        return matcher.matches();
    }

    public static boolean isCardId(String str){
        String regEx = "(^[1-9]\\d{5}(18|19|20)\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$)|(^[1-9]\\d{5}\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}$)";
        Pattern pattern = Pattern.compile(regEx);
        Matcher matcher = pattern.matcher(str);
        return matcher.matches();
    }

    public static boolean isMailRight(String str) {
        return regTest(str, 1);
    }

    public static boolean isPhoneNumberRight(String str) {
        return regTest(str, 0);
    }

    private static boolean regTest(String str, Integer regType) {
        String regEx = "";
        if (regType == 1) {
            regEx = "[0-9a-zA-Z_]{1,}@(([a-zA-z0-9]-*){1,}\\.){1,3}[a-zA-z\\-]{1,}";
        } else {
            regEx = "^1[3|4|5|8][0-9]\\d{8}$";
        }
        Pattern pattern = Pattern.compile(regEx);
        Matcher matcher = pattern.matcher(str);
        return matcher.matches();
    }


}
